package com.opc.controller;

import com.alibaba.excel.EasyExcel;
import com.opc.dto.HistoryRecordDto;
import com.opc.excel.HistoryExcel;
import com.opc.service.DeviceRecordService;
import com.opc.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@Slf4j
@CrossOrigin("*")
@RestController
@RequestMapping(("/excel"))
public class ExcelController {
    @Autowired
    private DeviceRecordService recordService;

    @GetMapping("/history_list")
    public void getHistoryListExcel(HttpServletResponse response, @RequestParam(name = "proLine", required = false) String proLine,
                                    @RequestParam(name = "startTime", required = false) String startTime,
                                    @RequestParam(name = "endTime", required = false) String endTime,
                                    @RequestParam(name = "valType", required = false, defaultValue = "2") String valType,
                                    @RequestParam(name = "recordType", required = false) String recordType) throws IOException {
        if (StringUtils.isBlank(startTime) || StringUtils.isBlank(endTime)) {
            return;
        }
        List<HistoryExcel> records = recordService.getHistoryListExcel(proLine, startTime, endTime, valType, recordType);
        if (records != null && !records.isEmpty()) {
            response.setContentType("application/vnd.ms-excel");
            response.setCharacterEncoding("utf-8");
            for (HistoryExcel record : records) {
                record.setStartTime(startTime);
                record.setEndTime(endTime);
            }
            EasyExcel.write(response.getOutputStream(), HistoryExcel.class).sheet("历史消耗").doWrite(records);
        }
    }

    @GetMapping("/abnormal_history_list")
    public void getAbnormalHistoryListExcel(HttpServletResponse response, @RequestParam(name = "proLine", required = false) String proLine,
                                            @RequestParam(name = "startTime", required = false) String startTime,
                                            @RequestParam(name = "endTime", required = false) String endTime,
                                            @RequestParam(name = "valType", required = false, defaultValue = "2") String valType,
                                            @RequestParam(name = "recordType", required = false) String recordType) throws IOException {
        List<HistoryRecordDto> records = recordService.getAbnormalHistoryExcel(proLine, startTime, endTime, valType, recordType);
        if (records != null && !records.isEmpty()) {
            response.setContentType("application/vnd.ms-excel");
            response.setCharacterEncoding("utf-8");
            EasyExcel.write(response.getOutputStream(), HistoryRecordDto.class).sheet("历史异常消耗").doWrite(records);
        }
    }


}
